package leetcode_1_200;

import java.util.*;

public class LeeCode_150 {
    public static void main(String[] args) {
        System.out.println(evalRPN(new String[]{"2", "1", "+", "3", "*"}));
        System.out.println(evalRPN(new String[]{"4", "13", "5", "/", "+"}));
    }
    private static int evalRPN(String[] tokens) {
        LinkedList<Integer> list = new LinkedList<>();
        for (String token : tokens) {
            int firN, secN;
            switch (token) {
                case "+": {
                    secN = list.getLast();
                    list.removeLast();
                    firN = list.getLast();
                    list.removeLast();
                    list.addLast(firN + secN);
                    break;
                }
                case "-": {
                    secN = list.getLast();
                    list.removeLast();
                    firN = list.getLast();
                    list.removeLast();
                    list.addLast(firN - secN);
                    break;
                }
                case "*": {
                    secN = list.getLast();
                    list.removeLast();
                    firN = list.getLast();
                    list.removeLast();
                    list.addLast(firN * secN);
                    break;
                }
                case "/": {
                    secN = list.getLast();
                    list.removeLast();
                    firN = list.getLast();
                    list.removeLast();
                    list.addLast(firN / secN);
                    break;
                }
                default:{
                    list.addLast(Integer.parseInt(token));
                    break;
                }
            }
        }
        return list.getLast();
    }
}
